<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>爬虫学习03——web请求全过程剖析 | XiaoZiYan</title><meta name="keywords" content="爬虫,python,web"><meta name="author" content="XiaoZiYan"><meta name="copyright" content="XiaoZiYan"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="python爬虫web">
<meta property="og:type" content="article">
<meta property="og:title" content="爬虫学习03——web请求全过程剖析">
<meta property="og:url" content="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan/2020/10/01/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A003%E2%80%94%E2%80%94web%E8%AF%B7%E6%B1%82%E5%85%A8%E8%BF%87%E7%A8%8B%E5%89%96%E6%9E%90/index.html">
<meta property="og:site_name" content="XiaoZiYan">
<meta property="og:description" content="python爬虫web">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://xiaoziyan.top/images/web.jpeg">
<meta property="article:published_time" content="2020-10-01T12:12:02.000Z">
<meta property="article:modified_time" content="2022-11-03T10:35:38.240Z">
<meta property="article:author" content="XiaoZiYan">
<meta property="article:tag" content="爬虫">
<meta property="article:tag" content="python">
<meta property="article:tag" content="web">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://xiaoziyan.top/images/web.jpeg"><link rel="shortcut icon" href="/xiaoziyan/img/favicon.png"><link rel="canonical" href="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan/2020/10/01/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A003%E2%80%94%E2%80%94web%E8%AF%B7%E6%B1%82%E5%85%A8%E8%BF%87%E7%A8%8B%E5%89%96%E6%9E%90/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><link rel="stylesheet" href="/xiaoziyan/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/xiaoziyan/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: XiaoZiYan","link":"链接: ","source":"来源: XiaoZiYan","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: true,
  islazyload: false,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '爬虫学习03——web请求全过程剖析',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-11-03 18:35:38'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="./css/custom.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome/css/font-awesome.min.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.css"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/xiaoziyan/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/xiaoziyan/archives/"><div class="headline">文章</div><div class="length-num">33</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/xiaoziyan/tags/"><div class="headline">标签</div><div class="length-num">41</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/xiaoziyan/categories/"><div class="headline">分类</div><div class="length-num">39</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/xiaoziyan/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 多媒体</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/xiaoziyan/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/xiaoziyan/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('http://xiaoziyan.top/images/web.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/xiaoziyan/">XiaoZiYan</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/xiaoziyan/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 多媒体</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/xiaoziyan/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/xiaoziyan/movies/"><i class="fa-fw fas fa-video"></i><span> 视频</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/link/"><i class="fa-fw fas fa-link"></i><span> 友情链接</span></a></div><div class="menus_item"><a class="site-page" href="/xiaoziyan/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">爬虫学习03——web请求全过程剖析</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-10-01T12:12:02.000Z" title="发表于 2020-10-01 20:12:02">2020-10-01</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-11-03T10:35:38.240Z" title="更新于 2022-11-03 18:35:38">2022-11-03</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/">爬虫学习</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/python/">python&quot;</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/python/%E7%88%AC%E8%99%AB/">爬虫</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/xiaoziyan/categories/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A0/python/%E7%88%AC%E8%99%AB/web/">web</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="爬虫学习03——web请求全过程剖析"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="web请求全过程剖析"><a href="#web请求全过程剖析" class="headerlink" title="web请求全过程剖析"></a>web请求全过程剖析</h2><p>上⼀⼩节我们实现了⼀个⽹⻚的整体抓取⼯作. 那么本⼩节, 给各位好好剖析⼀下web请求的全部过程, 这样有助于后⾯我们遇到的各种各样的⽹站就有了⼊⼿的基本准则了.</p>
<h4 id="那么到底我们浏览器在输⼊完⽹址到我们看到⽹⻚的整体内容-这个过程中究竟发⽣了些什么"><a href="#那么到底我们浏览器在输⼊完⽹址到我们看到⽹⻚的整体内容-这个过程中究竟发⽣了些什么" class="headerlink" title="那么到底我们浏览器在输⼊完⽹址到我们看到⽹⻚的整体内容, 这个过程中究竟发⽣了些什么?"></a>那么到底我们浏览器在输⼊完⽹址到我们看到⽹⻚的整体内容, 这个过程中究竟发⽣了些什么?</h4><p>这⾥我们以百度为例. 在访问百度的时候, 浏览器会把这⼀次请求发送到百度的服务器(百度的⼀台电脑), 由服务器接收到这个请求, 然后加载⼀些数据. 返回给浏览器, 再由浏览器进⾏显示. 听起来好像是个废话…但是这⾥蕴含着⼀个极为重要的东⻄在⾥⾯, 注意, 百度的服务器返回给浏览器的不直接是⻚⾯, ⽽是⻚⾯源代码(由html, css, js组成). 由浏览器把⻚⾯源代码进⾏执⾏, 然后把执⾏之后的结果展示给⽤户. 所以我们能看到在上⼀节的内容中,我们拿到的是百度的源代码(就是那堆看不懂的⻤东⻄). </p>
<h4 id="具体过程如图"><a href="#具体过程如图" class="headerlink" title="具体过程如图."></a>具体过程如图.</h4><p><img src="http://xiaoziyan.top/images/image-20210929201447237.png" alt="web解析全过程"></p>
<p>接下来就是⼀个⽐较重要的事情了. 所有的数据都在⻚⾯源代码⾥么? ⾮也~ 这⾥要介绍⼀个新的概念</p>
<p>那就是⻚⾯渲染数据的过程, 我们常⻅的⻚⾯渲染过程有两种,</p>
<h2 id="服务器渲染"><a href="#服务器渲染" class="headerlink" title="服务器渲染"></a>服务器渲染</h2><p>这个最容易理解, 也是最简单的. 含义呢就是我们在请求到服务器的时候, 服务器直接把数据全部写⼊到html中, 我们浏览器就能直接拿到带有数据的html内容. ⽐如,</p>
<p><img src="http://xiaoziyan.top/images/04.jpg" alt="服务器渲染"></p>
<h4 id="由于数据是直接写在html中的-所以我们能看到的数据都在⻚⾯源代码中能找的到的-这种⽹⻚⼀般都相对⽐较容易就能抓取到⻚⾯内容"><a href="#由于数据是直接写在html中的-所以我们能看到的数据都在⻚⾯源代码中能找的到的-这种⽹⻚⼀般都相对⽐较容易就能抓取到⻚⾯内容" class="headerlink" title="由于数据是直接写在html中的, 所以我们能看到的数据都在⻚⾯源代码中能找的到的.这种⽹⻚⼀般都相对⽐较容易就能抓取到⻚⾯内容."></a>由于数据是直接写在html中的, 所以我们能看到的数据都在⻚⾯源代码中能找的到的.这种⽹⻚⼀般都相对⽐较容易就能抓取到⻚⾯内容.</h4><h2 id="前端JS渲染"><a href="#前端JS渲染" class="headerlink" title="前端JS渲染"></a>前端JS渲染</h2><p>这种就稍显麻烦了. 这种机制⼀般是第⼀次请求服务器返回⼀堆HTML框架结构. 然后再次请求到真正保存数据的服务器, 由这个服务器返回数据, 最后在浏览器上对数据进⾏加载. 就像这样:</p>
<p><img src="http://xiaoziyan.top/images/image-20210929201841145.png" alt="前端js渲染逻辑图"></p>
<h4 id="这样做的好处是服务器那边能缓解压力-而且分工明确-比较容易维护-典型的有这么⼀个网页"><a href="#这样做的好处是服务器那边能缓解压力-而且分工明确-比较容易维护-典型的有这么⼀个网页" class="headerlink" title="这样做的好处是服务器那边能缓解压力. 而且分工明确. 比较容易维护. 典型的有这么⼀个网页"></a>这样做的好处是服务器那边能缓解压力. 而且分工明确. 比较容易维护. 典型的有这么⼀个网页</h4><p><img src="http://xiaoziyan.top/images/05.jpg" alt="京东页面示例"></p>
<p>那数据是何时加载进来的呢? 其实就是在我们进⾏⻚⾯向下滚动的时候, jd就在偷偷的加载数据了, 此时想要看到这个⻚⾯的加载全过程, 我们就需要借助浏览器的调试⼯具了(F12)</p>
<p><img src="http://xiaoziyan.top/images/06.jpg" alt="调试过程1"></p>
<p><img src="http://xiaoziyan.top/images/07.jpg" alt="调试过程2"></p>
<p><img src="http://xiaoziyan.top/images/08.jpg" alt="调试过程3"></p>
<p><img src="http://xiaoziyan.top/images/09.jpg" alt="调试过程4"></p>
<h4 id="看到了吧-页面上看到的内容其实是后加载进来的"><a href="#看到了吧-页面上看到的内容其实是后加载进来的" class="headerlink" title="看到了吧, 页面上看到的内容其实是后加载进来的."></a>看到了吧, 页面上看到的内容其实是后加载进来的.</h4><p>OK, 在这⾥我不是要跟各位讲有多⽜B, 也不是说这两种⽅式有什么不同, 只是想告诉各位, 有些时候, 我们的数据不⼀定都是直接来⾃于⻚⾯源代码. 如果你在⻚⾯源代码中找不到你要的数据时, 那很可能数据是存放在另⼀个请求⾥.</p>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">XiaoZiYan</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a target="_blank" rel="noopener" href="https://baidu.com">https://baidu.com</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://xiaoziyanxiaoludan.gitee.io/xiaoziyan" target="_blank">XiaoZiYan</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/xiaoziyan/tags/%E7%88%AC%E8%99%AB/">爬虫</a><a class="post-meta__tags" href="/xiaoziyan/tags/python/">python</a><a class="post-meta__tags" href="/xiaoziyan/tags/web/">web</a></div><div class="post_share"><div class="social-share" data-image="http://xiaoziyan.top/images/web.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/xiaoziyan/2020/10/02/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A004%E2%80%94%E2%80%94HTTP%E5%8D%8F%E8%AE%AE/"><img class="prev-cover" src="http://xiaoziyan.top/images/http.jpeg" onerror="onerror=null;src='/xiaoziyan/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">爬虫学习04——HTTP协议</div></div></a></div><div class="next-post pull-right"><a href="/xiaoziyan/2020/09/30/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A002%E2%80%94%E2%80%94%E7%99%BE%E5%BA%A6%E9%A1%B5%E9%9D%A2/"><img class="next-cover" src="http://xiaoziyan.top/images/baidu.jpeg" onerror="onerror=null;src='/xiaoziyan/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">爬虫学习02——爬整个百度页面</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/xiaoziyan/2020/09/30/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A002%E2%80%94%E2%80%94%E7%99%BE%E5%BA%A6%E9%A1%B5%E9%9D%A2/" title="爬虫学习02——爬整个百度页面"><img class="cover" src="http://xiaoziyan.top/images/baidu.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-09-30</div><div class="title">爬虫学习02——爬整个百度页面</div></div></a></div><div><a href="/xiaoziyan/2020/10/02/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A004%E2%80%94%E2%80%94HTTP%E5%8D%8F%E8%AE%AE/" title="爬虫学习04——HTTP协议"><img class="cover" src="http://xiaoziyan.top/images/http.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-02</div><div class="title">爬虫学习04——HTTP协议</div></div></a></div><div><a href="/xiaoziyan/2020/10/03/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A005%E2%80%94%E2%80%94requests%E6%A8%A1%E5%9D%97%E5%85%A5%E9%97%A8/" title="爬虫学习05——requests模块"><img class="cover" src="http://xiaoziyan.top/images/requests.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-03</div><div class="title">爬虫学习05——requests模块</div></div></a></div><div><a href="/xiaoziyan/2020/10/04/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A006%E2%80%94%E2%80%94%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/" title="爬虫学习06——正则表达式"><img class="cover" src="http://xiaoziyan.top/images/8f6cb19a9c1e6a44bcb597bd5d83cc23.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-04</div><div class="title">爬虫学习06——正则表达式</div></div></a></div><div><a href="/xiaoziyan/2020/10/06/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A008%E2%80%94%E2%80%94%E6%89%8B%E5%88%83%E8%B1%86%E7%93%A3TOP250%E7%94%B5%E5%BD%B1%E4%BF%A1%E6%81%AF/" title="爬虫学习08——豆瓣TOP250"><img class="cover" src="http://xiaoziyan.top/images/ceeee12fc03b31eda4dacc44aa21ee96.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-06</div><div class="title">爬虫学习08——豆瓣TOP250</div></div></a></div><div><a href="/xiaoziyan/2020/10/07/%E7%88%AC%E8%99%AB%E5%AD%A6%E4%B9%A009%E2%80%94%E2%80%94bs4%E8%A7%A3%E6%9E%90-HTML%E8%AF%AD%E6%B3%95/" title="爬虫学习09——bs4解析-HTML语法"><img class="cover" src="http://xiaoziyan.top/images/d9e428449c4bad8f74848c43d771f2ef.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-10-07</div><div class="title">爬虫学习09——bs4解析-HTML语法</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#web%E8%AF%B7%E6%B1%82%E5%85%A8%E8%BF%87%E7%A8%8B%E5%89%96%E6%9E%90"><span class="toc-number">1.</span> <span class="toc-text">web请求全过程剖析</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%82%A3%E4%B9%88%E5%88%B0%E5%BA%95%E6%88%91%E4%BB%AC%E6%B5%8F%E8%A7%88%E5%99%A8%E5%9C%A8%E8%BE%93%E2%BC%8A%E5%AE%8C%E2%BD%B9%E5%9D%80%E5%88%B0%E6%88%91%E4%BB%AC%E7%9C%8B%E5%88%B0%E2%BD%B9%E2%BB%9A%E7%9A%84%E6%95%B4%E4%BD%93%E5%86%85%E5%AE%B9-%E8%BF%99%E4%B8%AA%E8%BF%87%E7%A8%8B%E4%B8%AD%E7%A9%B6%E7%AB%9F%E5%8F%91%E2%BD%A3%E4%BA%86%E4%BA%9B%E4%BB%80%E4%B9%88"><span class="toc-number">1.0.1.</span> <span class="toc-text">那么到底我们浏览器在输⼊完⽹址到我们看到⽹⻚的整体内容, 这个过程中究竟发⽣了些什么?</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%B7%E4%BD%93%E8%BF%87%E7%A8%8B%E5%A6%82%E5%9B%BE"><span class="toc-number">1.0.2.</span> <span class="toc-text">具体过程如图.</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%B8%B2%E6%9F%93"><span class="toc-number">2.</span> <span class="toc-text">服务器渲染</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%94%B1%E4%BA%8E%E6%95%B0%E6%8D%AE%E6%98%AF%E7%9B%B4%E6%8E%A5%E5%86%99%E5%9C%A8html%E4%B8%AD%E7%9A%84-%E6%89%80%E4%BB%A5%E6%88%91%E4%BB%AC%E8%83%BD%E7%9C%8B%E5%88%B0%E7%9A%84%E6%95%B0%E6%8D%AE%E9%83%BD%E5%9C%A8%E2%BB%9A%E2%BE%AF%E6%BA%90%E4%BB%A3%E7%A0%81%E4%B8%AD%E8%83%BD%E6%89%BE%E7%9A%84%E5%88%B0%E7%9A%84-%E8%BF%99%E7%A7%8D%E2%BD%B9%E2%BB%9A%E2%BC%80%E8%88%AC%E9%83%BD%E7%9B%B8%E5%AF%B9%E2%BD%90%E8%BE%83%E5%AE%B9%E6%98%93%E5%B0%B1%E8%83%BD%E6%8A%93%E5%8F%96%E5%88%B0%E2%BB%9A%E2%BE%AF%E5%86%85%E5%AE%B9"><span class="toc-number">2.0.1.</span> <span class="toc-text">由于数据是直接写在html中的, 所以我们能看到的数据都在⻚⾯源代码中能找的到的.这种⽹⻚⼀般都相对⽐较容易就能抓取到⻚⾯内容.</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%89%8D%E7%AB%AFJS%E6%B8%B2%E6%9F%93"><span class="toc-number">3.</span> <span class="toc-text">前端JS渲染</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%BF%99%E6%A0%B7%E5%81%9A%E7%9A%84%E5%A5%BD%E5%A4%84%E6%98%AF%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%82%A3%E8%BE%B9%E8%83%BD%E7%BC%93%E8%A7%A3%E5%8E%8B%E5%8A%9B-%E8%80%8C%E4%B8%94%E5%88%86%E5%B7%A5%E6%98%8E%E7%A1%AE-%E6%AF%94%E8%BE%83%E5%AE%B9%E6%98%93%E7%BB%B4%E6%8A%A4-%E5%85%B8%E5%9E%8B%E7%9A%84%E6%9C%89%E8%BF%99%E4%B9%88%E2%BC%80%E4%B8%AA%E7%BD%91%E9%A1%B5"><span class="toc-number">3.0.1.</span> <span class="toc-text">这样做的好处是服务器那边能缓解压力. 而且分工明确. 比较容易维护. 典型的有这么⼀个网页</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%9C%8B%E5%88%B0%E4%BA%86%E5%90%A7-%E9%A1%B5%E9%9D%A2%E4%B8%8A%E7%9C%8B%E5%88%B0%E7%9A%84%E5%86%85%E5%AE%B9%E5%85%B6%E5%AE%9E%E6%98%AF%E5%90%8E%E5%8A%A0%E8%BD%BD%E8%BF%9B%E6%9D%A5%E7%9A%84"><span class="toc-number">3.0.2.</span> <span class="toc-text">看到了吧, 页面上看到的内容其实是后加载进来的.</span></a></li></ol></li></ol></li></ol></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By XiaoZiYan</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><p><a style="margin-inline:5px"target="_blank" href="https://hexo.io/"> <img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&logo=hexo" title="博客框架为 Hexo" alt="HEXO"></a><a style="margin-inline:5px"target="_blank"href="https://butterfly.js.org/"> <img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&logo=bitdefender" title="主题采用 Butterfly" alt="Butterfly"></a><a style="margin-inline:5px"target="_blank" href="https://www.jsdelivr.com/"> <img src="https://img.shields.io/badge/CDN-jsDelivr-orange?style=flat&logo=jsDelivr" title="本站使用 Jsdelivr 为静态资源提供CDN加速" alt="Jsdelivr"></a><a style="margin-inline:5px"target="_blank" href="https://github.com/"> <img src="https://img.shields.io/badge/Source-gitee-d021d6?style=flat&logo=Gitee" title="本站项目由 Gitee 托管" alt="Gitee"></a><a style="margin-inline:5px"target="_blank"href="http://creativecommons.org/licenses/by-nc-sa/4.0/"> <img src="https://img.shields.io/badge/Copyright-BY--NC--SA%204.0-d42328?style=flat&logo=Claris" alt="img" title="本站采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可"></a></p></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/xiaoziyan/js/utils.js"></script><script src="/xiaoziyan/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>function panguFn () {
  if (typeof pangu === 'object') pangu.autoSpacingPage()
  else {
    getScript('https://cdn.jsdelivr.net/npm/pangu/dist/browser/pangu.min.js')
      .then(() => {
        pangu.autoSpacingPage()
      })
  }
}

function panguInit () {
  if (false){
    GLOBAL_CONFIG_SITE.isPost && panguFn()
  } else {
    panguFn()
  }
}

document.addEventListener('DOMContentLoaded', panguInit)</script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script src="./js/sakura.js"></script><canvas class="fireworks" mobile="false"></canvas><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/fireworks.min.js"></script><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function electric_clock_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-clock"><div class="card-glass"><div class="card-background"><div class="card-content"><div id="hexo_electric_clock"><img id="card-clock-loading" src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-clock/clock/images/weather/loading.gif" style="height: 120px; width: 100%;" data-ll-status="loading" class="entered loading"></div></div></div></div></div>';
                console.log('已挂载electric_clock')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            electric_clock_injector_config()
        } </script><script src="https://pv.sohu.com/cityjson?ie=utf-8"></script><script data-pjax  src="https://cdn.jsdelivr.net/gh/Zfour/hexo-electric-clock@1.0.6/clock.js"></script><!-- hexo injector body_end end --></body></html>